Skip to content

Honor IncludeCategory in the JSON log format#2474

Open
TemRevil wants to merge 2 commits into
aws:devfrom
TemRevil:fix/json-log-include-category
Open

Honor IncludeCategory in the JSON log format#2474
TemRevil wants to merge 2 commits into
aws:devfrom
TemRevil:fix/json-log-include-category

Conversation

@TemRevil

Copy link
Copy Markdown

Fixes #2469

Problem

With Amazon.Lambda.Logging.AspNetCore, the ILogger category is written on every line in text format when LambdaLoggerOptions.IncludeCategory is true (which is the default). The JSON branch of LambdaILogger.Log, however, only forwards the message template and its arguments and never emits the category, so IncludeCategory is silently ignored in JSON mode. Migrating a function from text to JSON logging drops the category entirely, with no way to opt back in.

Change

When IncludeCategory is set, the JSON branch now prepends a {Category} placeholder to the message template and supplies the category as the matching argument. Because the JSON formatter turns named template parameters into top-level properties, the category surfaces as a queryable property (and in the rendered message), matching the parity the text format already provides.

  • Text format: unchanged.
  • JSON format with IncludeCategory = false: unchanged (no extra property, same parameter count).
  • JSON format with IncludeCategory = true: the category is included.

This follows the reporter's suggested "at minimum honor IncludeCategory" approach and needs no runtime-side change. If the maintainers would rather expose it as a dedicated top-level category field emitted by JsonLogMessageFormatter (the issue's preferred option), I'm happy to take that direction instead.

Testing

Amazon.Lambda.Logging.AspNetCore.Tests on net10.0: Passed! Failed: 0, Passed: 16. Added TestJSONParameterLoggingIncludesCategoryWhenEnabled (category present, parameter count goes 3 to 4) and TestJSONParameterLoggingOmitsCategoryWhenDisabled (no category, count stays 3); the existing TestJSONParameterLogging still passes since it uses IncludeCategory = false.

In text format the ILogger category is written on every line when
LambdaLoggerOptions.IncludeCategory is true (the default), but the JSON
branch of LambdaILogger.Log never emitted the category, so migrating a
function from text to JSON logging silently lost it with no way to opt
back in (aws#2469).

When IncludeCategory is set, prepend a {Category} placeholder to the
message template and supply the category value, so it surfaces as a
queryable property in the JSON record instead of being dropped. Text
format is unchanged, and JSON output is unchanged when IncludeCategory
is false.

Fixes aws#2469
@TemRevil TemRevil requested review from a team as code owners July 11, 2026 00:00
@TemRevil TemRevil requested review from GarrettBeatty and normj July 11, 2026 00:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates Amazon.Lambda.Logging.AspNetCore to honor LambdaLoggerOptions.IncludeCategory when Lambda JSON logging is enabled, so category information isn’t silently dropped when switching from text to JSON logs.

Changes:

  • Prepend a {Category} placeholder (and corresponding argument) to the JSON logging message template when IncludeCategory is enabled.
  • Add tests asserting JSON parameter logging includes/excludes category depending on IncludeCategory.
  • Add an autover changelog entry describing the fix.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
Libraries/src/Amazon.Lambda.Logging.AspNetCore/LambdaILogger.cs Adds category injection in the JSON logging branch when IncludeCategory is enabled.
Libraries/test/Amazon.Lambda.Logging.AspNetCore.Tests/LoggingTests.cs Adds tests validating category presence/absence impacts parameter count in JSON mode.
.autover/changes/541b1775-ecf8-4e75-99c8-675924a5c34e.json Records a patch changelog note for the logging fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +78 to +86
if (_options.IncludeCategory)
{
// Unlike the text format, the JSON format otherwise drops the
// category entirely, so IncludeCategory has no effect. Prepend a
// "{Category}" placeholder (which the JSON formatter turns into a
// queryable property) and supply the category value.
messageTemplate = "[{Category}] " + messageTemplate;
parameters.Insert(0, _categoryName);
}
@GarrettBeatty GarrettBeatty changed the base branch from master to dev July 15, 2026 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logger category is silently dropped in JSON log format (IncludeCategory has no effect)

2 participants